import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;


/**
 * Creates the dialog for adding a Doctor.
 * 
 * @author Lsun
 */
@SuppressWarnings("serial")
public class AddDoctorDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private final JTextField txtFirstName;
	private final JTextField txtLastName;
	private final JTextField txtUserName;
	private final JTextField txtPassword;
	private final JTextField txtSSN;
	private Connection con;

	/**
	 * Create the dialog.
	 * 
	 * @param mac The MedAppController that drives the dialog.
	 */
	public AddDoctorDialog(final MedAppController mac) {
		setTitle("AddDoctorDialog");
		con = mac.makeConnection();
		setBounds(100, 100, 686, 337);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		final JLabel lblFirstName = new JLabel("First Name:");
		lblFirstName.setBounds(37, 48, 114, 14);
		contentPanel.add(lblFirstName);

		final JLabel lblLastName = new JLabel("Last Name: ");
		lblLastName.setBounds(37, 88, 75, 14);
		contentPanel.add(lblLastName);

		final JLabel lblUsername = new JLabel("Username: ");
		lblUsername.setBounds(37, 132, 98, 14);
		contentPanel.add(lblUsername);

		final JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(37, 173, 98, 14);
		contentPanel.add(lblPassword);

		final JLabel lblSsn = new JLabel("SSN: ");
		lblSsn.setBounds(37, 217, 46, 14);
		contentPanel.add(lblSsn);

		txtFirstName = new JTextField();
		txtFirstName.setBounds(306, 40, 245, 22);
		contentPanel.add(txtFirstName);
		txtFirstName.setColumns(10);

		txtLastName = new JTextField();
		txtLastName.setBounds(306, 84, 245, 22);
		contentPanel.add(txtLastName);
		txtLastName.setColumns(10);

		txtUserName = new JTextField();
		txtUserName.setBounds(306, 132, 245, 22);
		contentPanel.add(txtUserName);
		txtUserName.setColumns(10);

		txtPassword = new JTextField();
		txtPassword.setBounds(306, 173, 245, 22);
		contentPanel.add(txtPassword);
		txtPassword.setColumns(10);

		txtSSN = new JTextField();
		txtSSN.setBounds(306, 217, 245, 22);
		contentPanel.add(txtSSN);
		txtSSN.setColumns(9);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					/**
					 * Designed to perform the action specified upon triggering.
					 * 
					 * @param arg0 The ActionEvent that triggers the method call.
					 */ 
					public void actionPerformed(ActionEvent arg0) {
						String firstName = txtFirstName.getText();
						String lastName = txtLastName.getText();
						String userName = txtUserName.getText();
						String password = txtPassword.getText();
						
						char[] pass = password.toCharArray();
						for (int i = 0; i < pass.length; i++) pass[i] += 7;
						String newPass = "";
						for (int i = pass.length - 1; i >= 0; i--) newPass += ((Character)pass[i]).toString();
						password = newPass;

						long ssn = Long.parseLong(txtSSN.getText());
						try {
							Statement stmt = con.createStatement();
							String sq1 = "INSERT INTO `velociraptorsattack`.`user` (`USERNAME`, `PASSWORD`, `U_FIRSTNAME`, `U_LASTNAME`, `USERTYPE`, `SUSPENDED`, `U_SSN`) VALUES ('" + userName + "', '" + password + "', '" + firstName + "', '" + lastName + "', 'Doctor', '0', '" + ssn + "')";
							String sq2 = "INSERT INTO `velociraptorsattack`.`doctor` (`D_FIRSTNAME`, `D_LASTNAME`, `D_SSN`) VALUES ('" + firstName + "', '" + lastName + "', '" + ssn + "')";
							stmt.executeUpdate(sq1);
							mac.out.println(sq1);
							stmt.executeUpdate(sq2);
							mac.out.println(sq2);
							Object[] options = {"Ok"};
							int dialog = JOptionPane.showOptionDialog(null, "Doctor Created!","Created!", JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE,null,options, null);
							if(dialog == JOptionPane.YES_OPTION){
								mac.setSysAdminPatientTableModel();
								dispose();
							}
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							JOptionPane.showMessageDialog(null, "Doctor Creation Error","Error!", JOptionPane.ERROR_MESSAGE);
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					/**
					 * Designed to perform the action specified upon triggering.
					 * 
					 * @param arg0 The ActionEvent that triggers the method call.
					 */ 
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
